<template>
  <div class="container">
    <!-- 页面权限点pid为0  type为1 -->
    <el-button v-permission=" 'add-employee' " type="primary" size="mini" @click="addPermission(0,1)">添加权限</el-button>
    <el-table
      style="width: 100%"
      :data="PermissionList"
      default-expand-all
      row-key="id"
    >
      <!-- row-key="id" 如没有 显示不出树形 -->
      <el-table-column
        prop="name"
        label="名称"
        align="center"
      />

      <el-table-column
        prop="code"
        label="标识"
        align="center"
      />
      <el-table-column
        prop="description"
        label="描述"
        align="center"
      />

      <el-table-column
        label="操作"
        align="center"
      >
        <template v-slot="{row}">
          <el-button v-if="row.type===1" type="text" @click="addPermission(row.id)">添加</el-button>
          <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
          <el-button type="text" @click="delPermission(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- .sync双向绑定dialog  替代了父组件@update=handle(title) title=true =>this.showDialog=true -->
    <el-dialog :title="`${showText}权限点`" :visible="showDialog">
      <!-- :model 绑定表单数据 -->
      <el-form ref="permissionForm" :model="permissionForm" style="margin-left:50px" :rules="rules">
        <el-form-item label="权限名称" prop="name" :show-dialog.sync="showDialog">
          <el-input v-model="permissionForm.name" style="width:500px" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="permissionForm.code" style="width:500px" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="permissionForm.description" style="width:500px" />
        </el-form-item>

        <el-form-item label="开启" prop="enVisible">
          <el-switch
            v-model="permissionForm.enVisible"
            active-color="#409eff"
            inactive-color="#dcdfe6"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>

        <el-row :gutter="10" type="flex" justify="center">
          <el-col :span="8">
            <el-button @click="btnCancel">取 消</el-button>
            <el-button type="primary" @click="btnOk()">确 定</el-button>
          </el-col>
        </el-row>

      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { addPermission, getPermissionList, updatePermission, delPermission, getPermissionDetail } from '@/api/permission'
import { transListToTreeData } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      showDialog: false,
      permissionForm: {
        name: '',
        description: '',
        type: '',
        code: '',
        pid: '',
        enVisible: ''
      },
      PermissionList: [],
      rules: {
        name: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.permissionForm.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表  但是要转为树形结构
    async getPermissionList() {
      // 不要忘了0
      this.PermissionList = transListToTreeData(await getPermissionList(), 0)
    },
    // 点击添加权限
    // 3. 注意：二级权限和一级权限的区别为type，当type为1时，为一级权限，当type为2时，为二级权限
    // 4. 二级权限应该添加在一级权限下，二者是通过id和pid进行关联的，其类似于组织架构中的添加子部门。
    // 二级权限的id=pid
    // 一级添加的type为1， 二级添加的type为2
    addPermission(pid, type) {
      this.permissionForm.pid = pid
      this.permissionForm.type = type
      this.showDialog = true
    },
    // 添加权限取消
    btnCancel() {
      this.permissionForm = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      }
      this.$refs.permissionForm.resetFields()
      this.showDialog = false
    },
    // 添加权限确定
    btnOk() {
      this.$refs.permissionForm.validate().then(() => {
        if (this.permissionForm.id) {
          return updatePermission(this.permissionForm)
        }
        return addPermission(this.permissionForm)
      }).then(() => {
        //  提示消息
        this.$message.success(`${this.showText}成功`)
        this.getPermissionList()
        this.showDialog = false
      })
    },
    // 编辑权限
    async editPermission(id) {
      // 根据获取id获取详情
      this.permissionForm = await getPermissionDetail(id)
      this.showDialog = true
    },
    // 删除操作
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除该数据吗')
        await delPermission(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    }
  }

}
// 父：.sync双向绑定dialog  替代了父组件@update=handle(title) title=false =>this.showDialog=false
// 子：this.$emit('update:showDialog',false)
</script>
